package com.ke.video_clip.demo

import android.content.Intent
import androidx.appcompat.app.AppCompatActivity
import android.os.Bundle
import android.widget.Button
import androidx.core.content.FileProvider
import androidx.viewbinding.BuildConfig
import com.ke.video_clip.lib.ClipVideo
import com.ke.video_clip.lib.ClipVideoRequest
import java.io.File

class MainActivity : AppCompatActivity() {
    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        setContentView(R.layout.activity_main)

        val launcher = registerForActivityResult(ClipVideo()) {

        }


        var savePath: String = ""

        File(externalCacheDir, "1.config").createNewFile()



        findViewById<Button>(R.id.view).setOnClickListener {
            val source = File(externalCacheDir, "09_58_05.TS").path
            val save = File(externalCacheDir, "${System.currentTimeMillis()}.mp4").path
            savePath = save

            launcher.launch(ClipVideoRequest(source, save))
        }

        findViewById<Button>(R.id.share).setOnClickListener {
            val intent = Intent(Intent.ACTION_SEND)
            val uri = FileProvider.getUriForFile(
                applicationContext,
                packageName + ".shareFileProvider",
                File(savePath)
            )
            intent.addFlags(Intent.FLAG_GRANT_READ_URI_PERMISSION)
            intent.putExtra(Intent.EXTRA_STREAM, uri)
            intent.type = "video/*"
            val chooser = Intent.createChooser(intent, "分享到")
            startActivity(chooser)
        }
    }
}